package com.lc.cehui.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.lc.cehui.R;
import com.lc.cehui.other.IntentKey;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ImageNineAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<Object> list;
    private boolean showDelete; //是否显示删除按钮
    public static final int TYPE_HEADER = 0; //说明是带有Header的
    public static final int TYPE_FOOTER = 1; //说明是带有Footer的
    public static final int TYPE_NORMAL = 2; //说明是不带有header和footer的


    public ImageNineAdapter(Context context, List<Object> list, boolean isShow) {
        this.context = context;
        this.list = list;
        this.showDelete=isShow;
    }


    public List<Object> getList() {
        return list;
    }

    /**获取File类型的图片
     * @return
     */
    public List<File> getFile(){
        List<File> file =new ArrayList<File>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) instanceof File){
                file.add((File) list.get(i));
            }
        }
        return file;
    }

    /**获取String 类型的图片
     * @return
     */
    public List<String> getString(){
        List<String> strings =new ArrayList<String>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) instanceof String){
                strings.add((String) list.get(i));
            }
        }
        return strings;
    }
    public void setList(List<Object> list) {
        this.list = list;
    }

    //HeaderView, FooterView
    private View mHeaderView;
    private View mFooterView;

    public View getmHeaderView() {
        return mHeaderView;
    }

    public void setmHeaderView(View mHeaderView) {
        this.mHeaderView = mHeaderView;
        notifyItemInserted(0);
    }

    public View getmFooterView() {
        return mFooterView;
    }

    public void setmFooterView(View mFooterView) {
        this.mFooterView = mFooterView;
        notifyItemInserted(getItemCount() - 1);
    }
    public void removeFooterView(){
        this.mFooterView=null;
    }


    /**
     * 重写这个方法，很重要，是加入Header和Footer的关键，我们通过判断item的类型，从而绑定不同的view *
     */
    @Override
    public int getItemViewType(int position) {
        if (mHeaderView == null && mFooterView == null) {
            return TYPE_NORMAL;
        }
        if (position == getItemCount() - 1) {
            //最后一个,应该加载Footer
            return TYPE_FOOTER;
        }
        return TYPE_NORMAL;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {

        if(mHeaderView != null && viewType == TYPE_HEADER) {
            return new ListHolder(mHeaderView);
        }
        if(mFooterView != null && viewType == TYPE_FOOTER){
            return new ListHolder(mFooterView);
        }

        View layout = LayoutInflater.from(context).inflate(R.layout.adapter_image_nine_item, viewGroup, false);
        return new ViewHolder(layout);

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        //recyclerView 高度自适应
        ViewGroup.LayoutParams layoutParams = viewHolder.itemView.getLayoutParams();
        layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;

            if (viewHolder instanceof ViewHolder) {
                //这里加载数据的时候要注意，是从position-1开始，因为position==0已经被header占用了
                setViewHolder((ViewHolder) viewHolder, i);
            }else if (viewHolder instanceof ListHolder){
                setListHolder((ListHolder) viewHolder, i);
            }

    }

    /**
     * 设置脚布局
     * @param viewHolder
     * @param i
     */
    private void setListHolder(ListHolder viewHolder, int i) {

    }

    /**
     * 设置item布局
     * @param viewHolder
     * @param i
     */
    private void setViewHolder(ViewHolder viewHolder, final int i) {
        if (showDelete)
            viewHolder.iv_delete.setVisibility(View.VISIBLE);
        else
        viewHolder.iv_delete.setVisibility(View.GONE);
        Glide.with(context).load(IntentKey.PAGEURL+list.get(i)).into(viewHolder.iv_photo);
        viewHolder.iv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onDeleteClickListener!=null){
                    onDeleteClickListener.onClick(v,i);
                }

            }
        });
        viewHolder.iv_photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               if (onPhotoClickListener!=null){
                   onPhotoClickListener.onClick(v,i);
               }
            }
        });

    }

    @Override
    public int getItemCount() {

        if (mHeaderView == null && mFooterView == null) {
            return list.size();
        } else if (mHeaderView == null && mFooterView != null) {
            return list.size() + 1;
        } else if (mHeaderView != null && mFooterView == null) {
            return list.size() + 1;
        } else {
            return list.size() + 2;
        }
    }
    public onPhotoClickListener onPhotoClickListener;
    public interface onPhotoClickListener{
        void onClick(View view, int position);
    }
    public void setOnPhotoClickListener(ImageNineAdapter.onPhotoClickListener onPhotoClickListener) {
        this.onPhotoClickListener = onPhotoClickListener;
    }

    public onDeleteClickListener onDeleteClickListener;
    public interface  onDeleteClickListener{
        void onClick(View view, int position);
    }
    public void  setonDeleteClickListener(onDeleteClickListener onDeleteClickListener){
        this.onDeleteClickListener=onDeleteClickListener;
    }
    class ListHolder extends RecyclerView.ViewHolder {
        ImageView iv_add;

        public ListHolder(View itemView) {
            super(itemView);
            //如果是headerview或者是footerview,直接返回
            if (itemView == mHeaderView) {
                return;
            }
            if (itemView == mFooterView) {
                return;
            }
            iv_add =  itemView.findViewById(R.id.iv_add);
        }
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_delete;
        ImageView iv_photo;
        ViewHolder(View view) {
            super(view);
            iv_delete = view.findViewById(R.id.iv_delete);
            iv_photo = view.findViewById(R.id.iv_photo);
        }
    }
}
